package com.example.larou.utils;

import cn.hutool.extra.mail.Mail;
import cn.hutool.extra.mail.MailAccount;
import com.example.larou.config.StateEnum;
import com.example.larou.entity.StateVo;
import com.example.larou.entity.User;
import com.sun.mail.util.MailSSLSocketFactory;

import java.security.GeneralSecurityException;

public class SendEmailCode {

    //初始化邮箱
    public static MailAccount init(){
        MailAccount account = new MailAccount();
        account.setHost("smtp.qq.com");
        account.setPort(465);
        account.setAuth(true);
        account.setFrom("1607264219@qq.com");
        account.setUser("1607264219@qq.com");
        account.setPass("miytzrpxnyosfeia");
        // ssl方式发送
        account.setSslEnable(true);
        // 使用STARTTLS安全连接
        account.setStarttlsEnable(true);
        return account;
    }

     public static Boolean sendEmail(StateVo stateVo){
        MailAccount accountInit=init();
        String email = stateVo.getEmail();
        String state=stateVo.getStateEnum().getName();
         //跳过ssl安全认证
         try {
             MailSSLSocketFactory sf  = new MailSSLSocketFactory();
             sf.setTrustAllHosts(true);
             accountInit.setCustomProperty("mail.smtp.ssl.socketFactory", sf);
             Mail mail=Mail.create(accountInit);
//             setContent(CodeGenerateUtil.generateVerCode())
             //邮箱内容
             mail.setTos(email)
                     .setTitle("报警装置异常警告")
                     .setContent("尊敬的车主先生/女士，\n\n"
                             + "我们向您发出这封紧急通知，因为我们监测到您的车辆内部还有人员滞留且"+ state + "异常升高。\n\n")
                     .setHtml(true)
                     .setUseGlobalSession(false)
                     .send();

             return true;
         } catch (GeneralSecurityException e) {
             e.printStackTrace();
             return false;
         }
     }
}
